package com.zkthink.ceres.promotion.controller;

import com.zkthink.ceres.promotion.entity.SalesPromConfig;
import com.zkthink.ceres.promotion.dto.SalesPromConfigSaveDTO;
import com.zkthink.ceres.promotion.dto.SalesPromConfigUpdateDTO;
import com.zkthink.ceres.promotion.dto.SalesPromConfigPageDTO;
import com.zkthink.ceres.promotion.service.SalesPromConfigService;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.zkthink.base.controller.SuperController;
import com.zkthink.base.R;
import com.zkthink.database.mybatis.conditions.Wraps;
import com.zkthink.database.mybatis.conditions.query.QueryWrap;
import com.zkthink.log.annotation.SysLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import com.zkthink.security.annotation.PreAuth;
import org.springframework.web.bind.annotation.RestController;


/**
 * <p>
 * 前端控制器
 * 分销员
 * </p>
 *
 * @author kellen
 * @date 2020-08-22
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/salesPromConfig")
@Api(value = "分销推广设置", tags = "分销推广设置")
//@PreAuth(replace = "salesPromConfig:")
public class SalesPromConfigController extends SuperController<SalesPromConfigService, Long, SalesPromConfig, SalesPromConfigPageDTO, SalesPromConfigSaveDTO, SalesPromConfigUpdateDTO> {

    /**
     * 管理后台查询 分销推广设置，如果没有默认数据，就初始化，其它地方查询需要考虑是否处理这种情况
     * @param data 批量查询
     * @return
     */
    @ApiOperation(value = "批量查询", notes = "批量查询")
    @PostMapping("/query")
    @SysLog("批量查询")
    @PreAuth("hasPermit('{}view')")
    public R<List<SalesPromConfig>> query(@RequestBody SalesPromConfig data) {
        QueryWrap<SalesPromConfig> wrapper = Wraps.q(data);
        List<SalesPromConfig> promConfigList = getBaseService().list(wrapper);
        if(CollectionUtils.isEmpty(promConfigList)){
            baseService.save(SalesPromConfig.builder().type(1).isShowShopLogo(1).isShowNickName(1).build());
            baseService.save(SalesPromConfig.builder().type(2).isShowShopLogo(1).isShowNickName(1).build());
            promConfigList = getBaseService().list(wrapper);
        }
        return success(promConfigList);
    }

}
